﻿using Direct.IDAL.Manager.Admin;
using DirectSell.Common.Url;
using DirectSell.Model.Manager;
using DirectSell.Model.Query;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.Manager.Admin
{
    /// <summary>
    /// 管理员仓储层
    /// </summary>
    public class AdminRepository:BaseManagerRepository, IAdminRepository
    {
        #region 查询管理员列表
        /// <summary>
        /// 查询管理员列表
        /// </summary>
        /// <param name="condition"></param>
        /// <returns></returns>
        public async Task<PagedList<Managers>> GetManagers(BaseCondition condition)
        {
            PageParam param = new PageParam();
            param.TableName = "Managers";
            param.PageIndex = condition.pageIndex==0?1:condition.pageIndex;
            param.PageSize = condition.pageSize;
            param.OrderBy = "Id";
            //StringBuilder sb = new StringBuilder();
            //sb.Append(" SELECT * FROM Managers WHERE 1=1 ");
            if (condition.SerachCondition != null)
            {
                param.StrWhere = $"(ManagerName LIKE '%{condition.SerachCondition.Trim()}%' OR ManagerCode  LIKE '%{condition.SerachCondition.Trim()}%' ) ";
                //sb.AppendFormat("AND (ManagerName LIKE '%{0}%' OR ManagerCode  LIKE '%{0}%' ) ", condition.SerachCondition.Trim());
            }
            return await base.PageQuery<Managers>(param);
            //var result = await base.QueryAsync<Managers>(sb.ToString());
            //return result as IList<Managers>;
        }
        #endregion

        #region 添加管理员
        /// <summary>
        /// 添加管理员
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public bool AddManager(ManagerReq req)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" INSERT INTO Managers ");
            sb.Append(" ([ManagerName] ");
            sb.Append(" ,[ManagerPassword] ");
            sb.Append(" ,[ManagerCode] ");
            sb.Append(" ,[Role]) ");
            sb.AppendFormat(" VALUES ");
            sb.AppendFormat(" ('{0}' ",req.ManagerName);
            sb.AppendFormat(" ,'{0}' ",req.ManagerPassword);
            sb.AppendFormat(" ,'{0}' ",req.ManagerCode);
            sb.AppendFormat(" ,'{0}') ",req.Role);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 判断管理员是否存在
        /// <summary>
        /// 判断管理员是否存在
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public bool IsExist(string usercode)
        {
            string sql = "SELECT COUNT(*) FROM Managers WHERE ManagerCode='{0}'";
            var count = base.QueryFirstT<int>(sql);
            return count <= 0;
        }
        #endregion

        #region 编辑
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public bool Eidt(ManagerReq req)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE Managers ");
            sb.AppendFormat(" SET [ManagerName] = '{0}' ",req.ManagerName);
            sb.AppendFormat(" ,[ManagerPassword] = '{0}' ",req.ManagerPassword);
            sb.AppendFormat(" ,[ManagerCode] = '{0}' ",req.ManagerCode);
            sb.AppendFormat("  WHERE Id={0} ",req.Id);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 查询一个管理员
        /// <summary>
        /// 查询一个管理员
        /// </summary>
        /// <returns></returns>
        public Managers QueryManagerById(int id)
        {
            string sql = string.Format(" SELECT * FROM Managers WHERE Id={0} ",id);
            return Query<Managers>(sql.ToString()).FirstOrDefault();
        }

        #endregion

        #region 删除
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            string sql = string.Format(" DELETE FROM Managers WHERE Id = {0} ", id);
            return base.Execute(sql.ToString());
        }
        #endregion

        #region 查询管理员数量
        /// <summary>
        /// 查询管理员数量
        /// </summary>
        /// <returns></returns>
        public bool CheckCount()
        {
            string sql = "SELECT COUNT(*) FROM Managers ";
            var count = base.QueryFirstT<int>(sql);
            return count > 1;
        }
        #endregion
    }
}
